package com.dlc.yunguang.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.utils.RecyclerViewUtil;
import com.dlc.commonlibrary.utils.TimeFormats;
import com.dlc.commonlibrary.utils.ToastUtil;
import com.dlc.commonlibrary.widget.CommonItemView;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.commonlibrary.widget.popup.CommonPopupList;
import com.dlc.yunguang.adapter.QuitHandworkAdapter;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.net.bean.HandworkBean;
import com.dlc.yunguang.net.bean.HotelBean;
import com.dlc.yunguang.net.protocol.OrderProto;
import com.dlc.yunguang.R;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import java.util.ArrayList;
import java.util.List;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class QuitHandworkActivity extends BaseActivity implements OnDateSetListener {


    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.tv_hotel)
    TextView tvHotel;
    @BindView(R.id.drop_hotel)
    FrameLayout dropHotel;
    @BindView(R.id.item_hotel)
    CommonItemView itemHotel;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    @BindView(R.id.start_time)
    CommonItemView startTime;
    @BindView(R.id.end_time)
    CommonItemView endTime;
    private QuitHandworkAdapter mQuitHandworkAdapter;

    private CommonPopupList mHotelPopup;//酒店
    List<HotelBean.DataBean> HotelList;
    private String HotelId = null;
    TimePickerDialog mDialogYearMonthDay;
    private int type = 0;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_quit_handwork;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        titleBar.leftImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        InitRecyclerView();
        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(this)
                .build();
    }

    @OnClick({R.id.drop_hotel, R.id.start_time, R.id.end_time,R.id.btn_quit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.drop_hotel:
                mHotelPopup.showAsDropDown(view);
                break;
            case R.id.start_time:
                type = 1;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
                break;
            case R.id.end_time:
                type = 2;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
                break;
            case R.id.btn_quit:
                mQuitHandworkAdapter.setNewData(null);
                get_record();
                break;
        }
    }

    private void InitRecyclerView() {
        mQuitHandworkAdapter = new QuitHandworkAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mQuitHandworkAdapter);

        mHotelPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropHotel)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        // TODO: 2017/7/20
                        tvHotel.setText(item);
                        if (HotelList != null) {
                            HotelId = HotelList.get(position).getId();
                        }

                    }
                })
                .build();

        getHotelPopupListData();


    }

    private void getHotelPopupListData() {
        OrderProto.get().getHotel().
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<HotelBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogPlus.e(e.getMessage());
                    }

                    @Override
                    public void onNext(HotelBean hotelBean) {
                        if (hotelBean != null && hotelBean.getData() != null) {
                            List<String> datas = new ArrayList<>();
                            HotelList = hotelBean.getData();
                            for (HotelBean.DataBean bean : hotelBean.getData()) {
                                datas.add(bean.getTitle());
                            }
                            mHotelPopup.setData(datas);
                        }
                    }
                });
    }


    private void get_record() {

        String stime ="",etime = "";
        stime= startTime.getContentText();
        etime = endTime.getContentText();
        if (!stime.contains("-"))
            stime = "";
        if (!etime.contains("-"))
            etime = "";
        if (TextUtils.isEmpty(HotelId)){
            ToastUtil.show(QuitHandworkActivity.this, getString(R.string.client_hint));
        }
        OrderProto.get().get_record(HotelId, stime, etime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HandworkBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast(e.getMessage());
                    }

                    @Override
                    public void onNext(HandworkBean dataBean) {
                        showOneToast(dataBean.msg);
                        mQuitHandworkAdapter.setNewData(dataBean.data);
                        // finish();
                    }
                });
    }


    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        if (type == 1){
            startTime.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
        }else {
            endTime.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
        }
    }
}
